package org.erepublik.company;

import org.json.JSONException;
import org.json.JSONObject;

public class Job implements JSONAble {

    private double salaryInGoldBefore;
    private double salaryInGoldAftetTax;
    private double salaryInLocalBeforeTax;
    private double incomeTax;
    private ECountry country;
    private long companyId;
    private int offerNumbers;
    private int minimalSkill;
//    private ESkill skill;
    private EProductType productType;
    private int quality;
    private String companyName;

    
    
    public double getSalaryInGoldAftetTax() {
        return salaryInGoldAftetTax;
    }
    public void setSalaryInGoldAftetTax(double salaryInGoldAftetTax) {
        this.salaryInGoldAftetTax = salaryInGoldAftetTax;
    }
    public EProductType getProductType() {
        return productType;
    }
    public void setProductType(EProductType productType) {
        this.productType = productType;
    }
    public int getQuality() {
        return quality;
    }
    public void setQuality(int quality) {
        this.quality = quality;
    }
    public String getCompanyName() {
        return companyName;
    }
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
    public double getSalaryInGoldBefore() {
        return salaryInGoldBefore;
    }
    public void setSalaryInGoldBefore(double salaryInGold) {
        this.salaryInGoldBefore = salaryInGold;
    }
    public double getSalaryInGoldAfterTax() {
        return Math.round(salaryInGoldAftetTax*100000.0)/100000.0d;
    }
    public void setSalaryInGoldAfterTax(double salary) {
        this.salaryInGoldAftetTax = salary;
    }
    public double getSalaryInLocalBeforeTax() {
        return Math.round(salaryInLocalBeforeTax*100.0)/100.0d;
    }
    public void setSalaryInLocalBeforeTax(double salaryWithTax) {
        this.salaryInLocalBeforeTax = salaryWithTax;
    }
    public int getIncomeTax() {
        return (int) Math.round( incomeTax*100.0d  );
    }
    public void setIncomeTax(double tax) {
        this.incomeTax = tax;
    }
    public ECountry getCountry() {
        return country;
    }
    public void setCountry(ECountry country) {
        this.country = country;
    }
    public long getCompanyId() {
        return companyId;
    }
    public void setCompanyId(long companyId) {
        this.companyId = companyId;
    }
    public int getOfferNumbers() {
        return offerNumbers;
    }
    public void setOfferNumbers(int offerNumbers) {
        this.offerNumbers = offerNumbers;
    }
    public int getMinimalSkill() {
        return minimalSkill;
    }
    public void setMinimalSkillByXP(int minimalSkill) {
        if (minimalSkill==0)
            this.minimalSkill=1;
        if (minimalSkill==20)
            this.minimalSkill=2;
        if (minimalSkill==100)
            this.minimalSkill=3;
        if (minimalSkill==500)
            this.minimalSkill=4;
        if (minimalSkill==2000)
            this.minimalSkill=5;
        if (minimalSkill==5000)
            this.minimalSkill=6;
        if (minimalSkill==10000)
            this.minimalSkill=7;
        if (minimalSkill==20000)
            this.minimalSkill=8;
        if (minimalSkill==40000)
            this.minimalSkill=9;
        if (minimalSkill==80000)
            this.minimalSkill=10;
        if (minimalSkill==160000)
            this.minimalSkill=11;
        if (minimalSkill==320000)
            this.minimalSkill=12;
    }
    
    public void setMinimalSkill(int minimalSkill) {
            this.minimalSkill=minimalSkill;
    }
    
    
//    public ESkill getSkill() {
//        return skill;
//    }
//    public void setSkill(ESkill skill) {
//        this.skill = skill;
//    }
    
    
    public JSONObject toJSONObject() throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("salaryInGold", salaryInGoldBefore);
        jsonObject.put("salary", salaryInGoldAftetTax);
        jsonObject.put("salaryWithTax", salaryInLocalBeforeTax);
        jsonObject.put("incomeTax", incomeTax);
        jsonObject.put("country", country);
        jsonObject.put("companyId", companyId);       
        jsonObject.put("offerNumbers", offerNumbers); 
        jsonObject.put("minimalSkill", minimalSkill);         
//        jsonObject.put("skill", skill); 
        jsonObject.put("quality", quality); 
        jsonObject.put("productType", productType); 
        jsonObject.put("companyName", companyName); 
        return jsonObject;
    }

    public static Job getJobFromJSON(JSONObject object) throws JSONException {

        Job job = new Job();
        
        if (object.get("salaryInGold") instanceof Integer)
            job.setSalaryInGoldBefore( (Integer)object.get("salaryInGold") );
        else
            job.setSalaryInGoldBefore( (Double)object.get("salaryInGold") );
        
        if (object.get("salary") instanceof Integer)
            job.setSalaryInGoldAfterTax( (Integer)object.get("salary") );
        else
            job.setSalaryInGoldAfterTax( (Double)object.get("salary") );
        
        if (object.get("salaryWithTax") instanceof Integer)
            job.setSalaryInLocalBeforeTax( (Integer)object.get("salaryWithTax") );
        else
            job.setSalaryInLocalBeforeTax( (Double)object.get("salaryWithTax") );
        
        if (object.get("incomeTax") instanceof Integer)
            job.setIncomeTax( (Integer)object.get("incomeTax") );
        else
            job.setIncomeTax( (Double)object.get("incomeTax") );
        
        job.setCountry( ECountry.valueOf( (String) object.get("country") ) );
        job.setCompanyId( (Integer)object.get("companyId") );
        job.setOfferNumbers( (Integer)object.get("offerNumbers") );
        job.setMinimalSkill( (Integer)object.get("minimalSkill") );
//        job.setSkill( ESkill.valueOf( (String) object.get("skill") ) );
        job.setQuality(  (Integer) object.get("quality")  );
        job.setProductType( EProductType.valueOf( (String) object.get("productType") ) );
        job.setCompanyName(  (String) object.get("companyName")  );
        
        return job;
    }
    
    
    public String getImg() {
        if (productType.isRaw())
            return "http://www.erepublik.com/images/icons/industry/"+productType.getErepublikId()+"/default.png";
        else 
            return "http://www.erepublik.com/images/icons/industry/"+productType.getErepublikId()+"/q"+getQuality()+".png";
    }
    
    
}
